"ui";
ui.layout(
    <vertical>
        <appbar>
            <toolbar title="乐萌自动看广告脚本"/>
        </appbar>
        <Switch id="autoService" text="必须开启无障碍服务" checked="{{auto.service != null}}" padding="8 8 8 8" textSize="15sp"/>
     
        <frame height="10" gravity="center">
        </frame> 
        <progressbar indeterminate="true" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"/>
        <linear>
        
        <checkbox id="" checked="true" size="12" color="#bb5500" text="自动看广告"/> 
        </linear>
        <progressbar indeterminate="true" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"/>
        <text id="_text"  text="乐萌自动看广告脚本  " textColor="#182AEB" textSize="20dp" gravity="center" />

        <button id="start" color="yellow" text="🔥开始运行🔥" layout_gravity="bottom" w="*" h="auto" style="Widget.AppCompat.Button.Colored" />
        {/*  <button id="expires" style="Widget.AppCompat.Button.Colored" text="联系我们"/> */}
    </vertical>

    

);
/* function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }
 */

function 滑动(x1, y1, x2, y2,time){
    if(device.sdkInt<24){
        Swipe(x1, y1, x2, y2,time)
    }else{
        swipe(x1, y1, x2, y2,time)
    }
}
function 点击(x,y){
    if(device.sdkInt<24){
        Tap(x,y)
    }else{
        click(x,y)
    }
}
/**
 *  不能点击的控件，通过坐标点击
 * clickUi(id("")); 
 *       clickUi(text("关闭应用"))
 * @param {*} uiSelector
 */

function clickUi(ui) {
    if (ui.exists()) {
        var a = ui.findOnce();
        if (a) {
            var b = a.bounds();
            if (b) {
                log(b)
                click(b.centerX(), b.centerY());
                return true;
            }
        }

    }
    return false;
}
ui.autoService.on("check", function(checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if(checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if(!checked && auto.service != null){
        auto.service.disableSelf();
    }
});

// 当用户回到本界面时，resume事件会被触发
ui.emitter.on("resume", function() {
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.autoService.checked = auto.service != null;
});

ui.start.on("click", function(){
    //程序开始运行之前判断无障碍服务
    if(auto.service == null) {
        toast("请先开启无障碍服务！");
        return;
    }
/* console.show();
console.setPosition(device.height/2, device.width/2); */
    threads.start(function () {
        toast("开始运行");
        auto.waitFor();
        home();
        sleep(2000);
        toast("打开应用中");
        app.launchApp("乐萌")
        
        while(true){
            clickUi(id("com.lemeng.rsqy:id/tv_letter_c")); //c
        toast("等待50s");
        sleep(10000);
        toast("等待40s");
        sleep(10000);
        toast("等待30s");
        sleep(10000);
        toast("等待20s");
        sleep(10000);
        toast("等待10s");
        sleep(10000);
       
        //点击关闭广告按钮
        
        /* 
        clickUi(id("com.lemeng.rsqy:id/tv_letter_g")); //g
        clickUi(id("com.lemeng.rsqy:id/tv_letter_g")); //g
        toast("等待50s");
        sleep(10000);
        toast("等待40s");
        sleep(10000);
        toast("等待30s");
        sleep(10000);
        toast("等待20s");
        sleep(10000);
        toast("等待10s");
        sleep(10000);
        console.error("点击关闭广告")
        //点击关闭广告按钮 */
        
        
        clickUi(id("com.lemeng.rsqy:id/tv_letter_h")); //h
        // clickUi(id("com.lemeng.rsqy:id/tv_letter_h")); //h
        toast("等待50s");
        sleep(10000);
        toast("等待40s");
        sleep(10000);
        toast("等待30s");
        sleep(10000);
        toast("等待20s");
        sleep(10000);
        toast("等待10s");
        sleep(10000);
        //console.error("点击关闭广告")
         //点击关闭广告按钮
        
            }

});
    threads.start(function () {
       toast("开启去广告线程")
        var imgim=className("android.widget.ImageView").indexInParent("1").depth("5").drawingOrder("2")
        while(true){
     clickUi(id("tt_video_ad_close_layout"));
     sleep(3000);
     clickUi(id("cancel_close"));
     sleep(3000)
     clickUi(id("cancel"));
    sleep(3000)
    
    if(imgim.exists()){
    
        clickUi(imgim)
    }
        }
    });
});

/* function main() {
    // 这里写脚本的主逻辑
 


} */